package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    private UserService userService;


    @RequestMapping("/getUsername.do")
    public Result getUsername() {
        User user = null;
        try {
            user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
        return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, user.getUsername());
    }

    //    添加用户
    @RequestMapping("/add.do")
    public Result add(@RequestBody com.itheima.pojo.User user, Integer[] roleIds) {

        Integer count = null;
        try {
            count = userService.add(user, roleIds);
            if (count > 0) {
                return new Result(true, MessageConstant.ADD_USER_SUCCESS);
            } else {
                return new Result(false, MessageConstant.ADD_USER_FAIL, "网络繁忙，请稍后再试");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_USER_FAIL, e.getMessage());
        }



    }
    //分页查询
    @RequestMapping("/findPage.do")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult = userService.findPage(queryPageBean);
            return new Result(true,"查询数据成功",pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询数据失败","网络繁忙，请稍后再试");
        }
    }
    //根据id查询
    @RequestMapping("/findById/{id}.do")
    public Result findById(@PathVariable Integer id){
        try {
            com.itheima.pojo.User user = userService.findById(id);
            return new Result(true,"查询数据成功",user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询数据失败","网络繁忙，请稍后再试");
        }

    }
    //根据id删除
    @RequestMapping("/deleteById/{id}.do")
    public Result deleteById(@PathVariable Integer id){
        try {
            Integer count = userService.deleteById(id);
            if (count >0){
                return new Result(true,"删除用户成功");
            }else {
                return new Result(false,"删除用户失败","网络繁忙，请稍后再试");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除用户失败",e.getMessage());
        }

    }
    //编辑用户
    @RequestMapping("/edit.do")
    public Result edit(@RequestBody com.itheima.pojo.User user,Integer[] roleIds){
        Integer count = null;
        try {
            count = userService.edit(user, roleIds);
            if (count > 0) {
                return new Result(true, MessageConstant.EDIT_USER_SUCCESS);
            } else {
                return new Result(false, MessageConstant.EDIT_USER_FAIL, "网络繁忙，请稍后再试");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_USER_FAIL, e.getMessage());
        }

    }



}
